package com.viyu.alianlk.icons.views;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;

import com.viyu.alianlk.icons.R;

/**
 * 
 * @author Viyu_Lu
 * 
 */
public class GameBoardView extends View {

	/**
	 * [[0, 0, 0, 0, 0, 0, 0, 0], 
		[0, 1, 6, 2, 10, 11, 5, 0], 
		[0, 6, 8, 8, 4, 7, 11, 0], 
		[0, 7, 10, 4, 1, 2, 12, 0], 
		[0, 9, 3, 12, 5, 9, 3, 0], 
		[0, 0, 0, 0, 0, 0, 0, 0]]
 		一下xCount和要yCount都减1才是真正能画出来的图标数目，因为外面一圈都是0，是画不出来的
	 */
	// count of x way
	public static int X_COUNT = 9;
	// count of y way
	public static int Y_COUNT = 12;
	// count of icons
	public static int ICON_COUNT = 15;
	// bitmap icons
	protected Bitmap[] icons = new Bitmap[ICON_COUNT];

	//
	protected int expanWhenSelected = 0;
	
	// matrix
	protected int[][] xYMatrix = new int[X_COUNT][Y_COUNT];
	// size of icon
	protected int iconSize = 0;

	// path to lian
	private Point[] path = null;
	// the point of icons selected
	protected List<Point> selectedIconsPointList = new ArrayList<Point>();

	public GameBoardView(Context context, AttributeSet atts) {
		super(context, atts);
		//
		calcIconSize();
		//
		Resources r = getResources();
		loadBitmaps(0, r.getDrawable(R.drawable.icon01));
		loadBitmaps(1, r.getDrawable(R.drawable.icon02));
		loadBitmaps(2, r.getDrawable(R.drawable.icon03));
		loadBitmaps(3, r.getDrawable(R.drawable.icon04));
		loadBitmaps(4, r.getDrawable(R.drawable.icon05));
		loadBitmaps(5, r.getDrawable(R.drawable.icon06));
		loadBitmaps(6, r.getDrawable(R.drawable.icon07));
		loadBitmaps(7, r.getDrawable(R.drawable.icon08));
		loadBitmaps(8, r.getDrawable(R.drawable.icon09));
		loadBitmaps(9, r.getDrawable(R.drawable.icon10));
		loadBitmaps(10, r.getDrawable(R.drawable.icon11));
		loadBitmaps(11, r.getDrawable(R.drawable.icon12));
		loadBitmaps(12, r.getDrawable(R.drawable.icon13));
		loadBitmaps(13, r.getDrawable(R.drawable.icon14));
		loadBitmaps(14, r.getDrawable(R.drawable.icon15));
	}

	/**
	 * calc the size of icon
	 */
	private void calcIconSize() {
		DisplayMetrics dm = new DisplayMetrics();
		((Activity) this.getContext()).getWindowManager().getDefaultDisplay().getMetrics(dm);
		iconSize = dm.widthPixels / (X_COUNT);
		expanWhenSelected = iconSize / 15;//选中时，周边扩展icon的1/15
		if(expanWhenSelected < 5) 
			expanWhenSelected = 5;//最小为5
	}

	/**
	 * load bitmap of icons
	 * 
	 * @param key
	 * @param d
	 */
	public void loadBitmaps(int key, Drawable d) {
		Bitmap bitmap = Bitmap.createBitmap(iconSize, iconSize, Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas(bitmap);
		d.setBounds(0, 0, iconSize, iconSize);
		d.draw(canvas);
		icons[key] = bitmap;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// draw path
		if (path != null && path.length >= 2) {
			for (int i = 0; i < path.length - 1; i++) {
				Paint paint = new Paint();
				paint.setColor(Color.LTGRAY);
				paint.setStyle(Paint.Style.STROKE);
				paint.setStrokeWidth(3);
				Point p1 = indextoScreen(path[i].x, path[i].y);
				Point p2 = indextoScreen(path[i + 1].x, path[i + 1].y);
				canvas.drawLine(p1.x + iconSize / 2, p1.y + iconSize / 2, p2.x + iconSize / 2, p2.y + iconSize / 2, paint);
			}
			Point p = path[0];
			xYMatrix[p.x][p.y] = 0;
			p = path[path.length - 1];
			xYMatrix[p.x][p.y] = 0;
			selectedIconsPointList.clear();
			path = null;
		}
		// draw icons
		for (int x = 0; x < xYMatrix.length; x += 1) {
			for (int y = 0; y < xYMatrix[x].length; y += 1) {
				if (xYMatrix[x][y] > 0) {
					Point p = indextoScreen(x, y);
					canvas.drawBitmap(icons[xYMatrix[x][y]], p.x, p.y, null);
				}
			}
		}

		// draw selected icons
		for (Point position : selectedIconsPointList) {
			Point p = indextoScreen(position.x, position.y);
			if (xYMatrix[position.x][position.y] >= 1) {
				Paint paint = new Paint();
				paint.setColor(Color.GREEN);
				paint.setStyle(Paint.Style.STROKE);
				paint.setStrokeWidth(3);
				Rect rect = new Rect(p.x - expanWhenSelected, p.y - expanWhenSelected, p.x + iconSize + expanWhenSelected, p.y + iconSize + expanWhenSelected);
				canvas.drawRect(rect, paint);
				canvas.drawBitmap(icons[xYMatrix[position.x][position.y]], null, rect, null);
			}
		}
	}

	public void drawLine(Point[] path) {
		this.path = path;
		this.invalidate();
	}

	public Point indextoScreen(int x, int y) {
		return new Point(x * iconSize, y * iconSize);
	}

	public Point screenToindex(int x, int y) {
		int ix = x / iconSize;
		int iy = y / iconSize;
		if (ix < X_COUNT && iy < Y_COUNT) {
			return new Point(ix, iy);
		} else {
			return new Point(0, 0);
		}
	}
}
